<?php
require_once 'setting.php';

require_once 'functions.php';

require_once INCLUDESPATH . 'db/User.php';
require_once INCLUDESPATH . 'db/UserDAO.php';
require_once INCLUDESPATH . 'db/Word.php';
require_once INCLUDESPATH . 'db/WordDAO.php';

class PagingWords {

	public function PagingWords($currentpage, $status) {
		$this->currentpage = $currentpage;
		$this->status = $status;
	}

	public function getPagingWords() {
		if (!($this->flag)) {
			$this->run();
		}
		return $this->pagingwords;
	}

	public function getWordsAmount() {
		if (!($this->flag)) {
			$this->run();
		}
		return $this->wordsamount;
	}

	public function getCurrentPage() {
		return $this->currentpage;
	}
	
	public function getPagesAmount() {
		if (!($this->flag)) {
			$this->run();
		}
		return $this->pagesamount;
	}

	/**
	 *
	 * Enter description here ...
	 * @return array 一个链接的数组，包含以下元素
	 * 	first
	 * 	prev
	 * 	currentpage
	 * 	pagesAmount
	 * 	next
	 * 	last
	 */
	public function getPagingLinks() {
		if (!($this->flag)) {
			$this->run();
		}
		return $this->paginglinks;
	}

	/**
	 * 
	 */
	private function run() {
		global $wordsperpage;

		$worddao = new WordDAO();
		# 获取所有的满足条件的单词的数量
		$userid = getCurrentUserId();
		if ($userid == -1) {
			die();
		}
		$params = array ('userid' => $userid, 'status' => $this->status);
		$this->wordsamount = intval($worddao->statistic($params));
		# 计算所有的页面数
		if ($this->wordsamount) {
			if ($this->wordsamount % $wordsperpage) {
				$this->pagesamount = (int)($this->wordsamount / $wordsperpage) + 1;
			} else {
				$this->pagesamount = $this->wordsamount / $wordsperpage;
			}
		} else {
			$this->pagesamount = 0;
		}
//		testToDie(array('wordsamount' => $this->wordsamount,
//		'pagesamount' => $this->pagesamount));
		# 获取当前页面的单词
		if (($this->currentpage) > ($this->pagesamount)) {
			$this->currentpage = $this->pagesamount;
		}
		$properties = array('userid' => $userid, 'status' => $this->status);
		$this->pagingwords = $worddao->retrieveByProperties($properties, 'addtime', 2, $this->currentpage, $wordsperpage);
		
		# 获取分页链接
		if ($this->currentpage == 1 || $this->currentpage == 0) {
			$this->paginglinks['first'] = '#';
			$this->paginglinks['prev'] = '#';
		} else {
			$this->paginglinks['first'] = '?page=1';
			$this->paginglinks['prev'] = '?page=' . ($this->currentpage - 1);
		}

		if ($this->currentpage == $this->pagesamount || $this->currentpage == 0) {
			$this->paginglinks['next'] = '#';
			$this->paginglinks['last'] = '#';
		} else {
			$this->paginglinks['next'] = '?page=' . ($this->currentpage + 1);
			$this->paginglinks['last'] = '?page=' . $this->pagesamount;
		}
		
		# 置flag值为1
		$this->flag = 1;
		
//		testToDie(array('flag' => $this->flag, 
//		'currentpage' => $this->currentpage,
//		'status' => $this->status,
//		'wordsamount' => $this->wordsamount,
//		'pagesamount' => $this->pagesamount,
//		'paginglink' => $this->paginglinks));
	}

	# 判断是否已经运算过的标志
	private $flag = 0;
	# 当前页面的编号
	private $currentpage = 0;
	# 当前要查询的单词的状态
	private $status = 'inbox';
	# 当前页面的单词的数组
	private $pagingwords = array();
	# 全部单词的数量
	private $wordsamount = 0;
	# 全部页面的数量
	private $pagesamount = 0;
	# 当前页面的更换页面链接
	private $paginglinks = array (  'first' => '#',
									'prev' => '#',
									'next' => '#',
									'last' => '#');
}

?>